package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Employee;
import com.manong.query.EmployeeQueryObject;
import com.manong.query.QueryObject;
import com.manong.service.IEmployeeService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import com.manong.vo.AdminVo;
import com.manong.vo.EmployeeRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/employee")
@CrossOrigin(allowCredentials = "true")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 分页查询接口
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "员工列表", expression = "employee:list")
    public JsonResult list(EmployeeQueryObject queryObject) {
        PageInfo pageInfo = employeeService.query(queryObject);
        // 先解决JsonResult->Data属性解决
        ResultData data = new ResultData(queryObject.getPageNum(), queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal() + ""));
        return JsonResult.success(data);
    }

    /**
     * 删除部门接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    public JsonResult delete(@PathVariable Long id) {
        employeeService.delete(id);
        return JsonResult.success();
    }

    /**
     * 员工新增或者编辑接口
     *
     * @param employeeRoleVo
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增或者编辑", expression = "employee:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody EmployeeRoleVo employeeRoleVo) {
        employeeService.saveOrUpdate(employeeRoleVo);
        return JsonResult.success();
    }

    /**
     * 查询单个员工
     * @param id
     * @return
     */
    @GetMapping("/getEmployee/{id}")
    @RequiredPermission(name = "查询单个员工", expression = "employee:getEmployee")
    public JsonResult getEmployee(@PathVariable Long id) {
        Employee employee = employeeService.get(id);
        // JsonResult data属性 employee
        return JsonResult.success(employee);
    }


    @GetMapping("/getEmployeeList")
    @RequiredPermission(name = "查询全部员工", expression = "employee:getEmployeeList")
    public JsonResult getEmployeeList() {
        List<Employee> all = employeeService.getAll();
        return JsonResult.success(all);
    }

    @PostMapping("/updateState")
    @RequiredPermission(name = "更新管理员状态", expression = "employee:updateState")
    public JsonResult updateState(@RequestBody AdminVo adminVo) {
        employeeService.updateState(adminVo);
        return JsonResult.success();
    }
}
